package com.example.listgame;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class ListGameActivity extends AppCompatActivity {

    private int[] picture = new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.margin};
    private GridView mGridView;
    final List<Map<String,Object>> listitem = new ArrayList<Map<String,Object>>();
    int temp = 0;
    int lastClicked;
    int numcolum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_game);
        mGridView = findViewById(R.id.gv);
        for (int i=0 ; i<8 ; i++){
            int num = (int)(Math.random()*4);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("image",picture[num]);
            listitem.add(map);
            listitem.add(map);
        }
        Collections.shuffle(listitem);
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitem,R.layout.layout_grid_view,new String[]{"image"},new int[]{R.id.image});
        mGridView.setAdapter(simpleAdapter);
        //建立点击事件
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Toast.makeText(ListGameActivity.this, "点击" + i, Toast.LENGTH_SHORT).show();
                int temp2 = (int) (listitem.get(i).get("image"));
                //第一个条件是判断点击的不是空的，第二个确认两次点击的不是同一个
                if (temp2 != R.drawable.margin && lastClicked != i) {
                    if (temp == 0) {
                        temp = temp2;
                    } else {
                        if (temp == temp2) {
                            //点击的图案相同
                            Point thispoint = arg2topoint(i);
                            Point lastpoint = arg2topoint(lastClicked);
                            if (CheckIsItCanBeDestoryed(thispoint, lastpoint)) {
                                Clear(i);
                                Clear(lastClicked);
                                checkIsSuccess();
                            }
                        }
                        temp = 0;
                    }
                    lastClicked=i;
                }
            }

        });
    }

    //存储坐标点的类
     class Point{
         int x;
         int y;
         Point(int px,int py){
                         x=px;
                         y=py;
                     }
         Point(Point p){
                         x=p.x;
                         y=p.y;
                     }
    }

    //把数字转换为坐标点
     private Point arg2topoint(int a){
                 int px=a%6;
                 int py=a/6;
                 return new Point(px, py);
             }

   //把点转换为数字
             private int pointtoarg2(Point a){
                 return a.y*6+a.x;
             }

    //是否已经全部消除
      private void checkIsSuccess() {
                 for (Map<String, Object> amap : listitem) {
                         if ((int)amap.get("image")!=R.drawable.margin) {
                                 return;
                             }
                     }
                 new AlertDialog.Builder(ListGameActivity.this).setTitle("胜利了！").setIcon(android.R.drawable.ic_dialog_alert)
                 .setMessage("你赢了，是不是特别有成就感呢!!!").setPositiveButton("OK", new DialogInterface.OnClickListener() {

                      @Override
              public void onClick(DialogInterface arg0, int arg1) {
                                 Intent intent=new Intent();
                                 intent.setClass(ListGameActivity.this, MainActivity.class);
                                 startActivity(intent);
                                 ListGameActivity.this.finish();
                             }
          }).setCancelable(false).show();
             }

      //判断是否可以消除
              private boolean CheckIsItCanBeDestoryed(Point p1,Point p2){
                 /*判断一条线可以连接的情况*/
                 if (testVertical(new Point(p1), new Point(p2))) {
                         return true;
                     }
                 if (testHorizontal(new Point(p1), new Point(p2))) {
                         return true;
                     }
                 /*判断两条线可以连接的情况*/
                 Point newPoint1=new Point(p2.x, p1.y);
                 int tmp1=pointtoarg2(newPoint1);
                 if ((int)listitem.get(tmp1).get("image")==R.drawable.margin) {
                         if (testVertical(p2, new Point(newPoint1))&&testHorizontal(p1, new Point(newPoint1))) {
                                 return true;
                             }
                     }
                 Point newPoint2=new Point(p1.x, p2.y);
                 tmp1=pointtoarg2(newPoint2);
                 if ((int)listitem.get(tmp1).get("image")==R.drawable.margin) {
                         if (testVertical(p1, new Point(newPoint2))&&testHorizontal(p2, new Point(newPoint2))) {
                                 return true;
                             }
                     }
                 /*判断三条线可以连接的情况*/
                 Vector<Line> vector=new Vector<ListGameActivity.Line>();
                 vector=Scan(new Point(p1), new Point(p2));
                 if (!vector.isEmpty()) {
                         for (int i = 0; i < vector.size(); i++) {
                                 Line line=vector.elementAt(i);
                                 //横线
                                 if (line.dirct==0) {
                                         if (testVertical(new Point(p1), new Point(line.a))&&testVertical(new Point(p2), new Point(line.b))) {
                                                 return true;
                                             }
                                     }else {
                                         if (testHorizontal(new Point(p1), new Point(line.a))&&testHorizontal(new Point(p2), new Point(line.b))) {
                                                 return true;
                                             }
                                     }
                             }
                     }

                 return false;
             }

     private Vector<Line> Scan(Point p1,Point p2) {
                 Vector<Line> v=new Vector<ListGameActivity.Line>();
                 //查找A左边的线
                 for (int y = p1.y; y >=0; y--) {
                         if ((int)listitem.get(pointtoarg2(new Point(p1.x, y))).get("image")==R.drawable.margin&&
                                         (int)listitem.get(pointtoarg2(new Point(p2.x, y))).get("image")==R.drawable.margin&&
                                         testHorizontal(new Point(p1.x,y), new Point(p2.x,y))) {
                                 v.add(new Line(0, new Point(p1.x,y), new Point(p2.x,y)));
                             }
                     }
                 //查找A右边边的线
                 for (int y = p1.y; y <4; y++) {
                         if ((int)listitem.get(pointtoarg2(new Point(p1.x, y))).get("image")==R.drawable.margin&&
                                         (int)listitem.get(pointtoarg2(new Point(p2.x, y))).get("image")==R.drawable.margin&&
                                         testHorizontal(new Point(p1.x,y), new Point(p2.x,y))) {
                                 v.add(new Line(0, new Point(p1.x,y), new Point(p2.x,y)));
                             }
                     }
                 //查找A上面的线
                 for (int x = p1.x; x >=0; x--) {
                         if ((int)listitem.get(pointtoarg2(new Point(x,p1.y))).get("image")==R.drawable.margin&&
                                         (int)listitem.get(pointtoarg2(new Point(x, p2.y))).get("image")==R.drawable.margin&&
                                         testVertical(new Point(x,p1.y), new Point(x,p2.y))) {
                                 v.add(new Line(1, new Point(x,p1.y), new Point(x,p2.y)));
                             }
                     }
                 //查找A下面的线
                 for (int x = p1.x; x <6; x++) {
                         if ((int)listitem.get(pointtoarg2(new Point(x,p1.y))).get("image")==R.drawable.margin&&
                                         (int)listitem.get(pointtoarg2(new Point(x, p2.y))).get("image")==R.drawable.margin&&
                                         testVertical(new Point(x,p1.y), new Point(x,p2.y))) {
                                 v.add(new Line(1, new Point(x,p1.y), new Point(x,p2.y)));
                             }
                     }
                 return v;
             }

     //判断是否可以用竖线链接两个点
             private boolean testVertical(Point p1,Point p2) {
                 //定义一个bool值，表示循环过程中是否碰到不为空的
                 boolean b=true;
                 if (p1.x==p2.x) {
                         //差值，循环时用到
                         int temp=(p1.y-p2.y)/Math.abs(p1.y-p2.y);
                         while(p1.y!=p2.y){
                                 p2.y+=temp;
                                 int arg2=pointtoarg2(p2);
                                 //如果对应坐标点不为空
                                 if((int)listitem.get(arg2).get("image")!=R.drawable.margin&&p1.y!=p2.y){
                                         b=false;
                                         break;
                                     }
                             }
                     }else {
                         b=false;
                     }
                 return b;
             }

     //判断是否可以用横线链接两个点
             private boolean testHorizontal(Point p1,Point p2) {
                 //定义一个bool值，表示循环过程中是否碰到不为空的
                 boolean b=true;
                if (p1.y==p2.y) {
                         //差值，循环时用到
                         int temp=(p1.x-p2.x)/Math.abs(p1.x-p2.x);
                         while(p1.x!=p2.x){
                                 p2.x+=temp;
                                 int arg2=pointtoarg2(p2);
                                 //如果对应坐标点不为空
                                 if((int)listitem.get(arg2).get("image")!=R.drawable.margin&&p1.x!=p2.x){
                                         b=false;
                                         break;
                                     }
                             }
                     }else {
                         b=false;
                     }
                 return b;
             }

    //消去某个，即为替换为空图像R.drawable.ull
     private void Clear(int x) {
                 HashMap<String, Object> hMap=new HashMap<String, Object>();
                 hMap.put("image", R.drawable.margin);
                 listitem.set(x, hMap);
             }

    //这个用来判断三条直线链接的时候用到
     class Line{
         Point a,b;
         int dirct;//1表示竖线，0表示横线
         public Line(int dirce,Point a,Point b) {
                         this.a=a;
                         this.b=b;
                         this.dirct=dirce;
                     }
     }
 }

